package edu.sctu.lab03;

import org.apache.catalina.Group;
import org.apache.catalina.Role;
import org.apache.catalina.User;
import org.apache.catalina.UserDatabase;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2018/6/2.
 */
public class Database {


    public List<User> getUserList() {
        List<User> userList = new ArrayList<User>();

        // 3 + 1
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        String sql = "select * from t_users";

        // 加载驱动程序
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC", // 数据库连接字符串
                    "root",
                    "123456" // 123456
            );

            statement = connection.createStatement();

            resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {

                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String password = resultSet.getString(3);

                User user = new User() {
                    @Override
                    public boolean equals(Object another) {
                        return false;
                    }

                    @Override
                    public String toString() {
                        return null;
                    }

                    @Override
                    public int hashCode() {
                        return 0;
                    }

                    @Override
                    public String getName() {
                        return null;
                    }

                    @Override
                    public String getFullName() {
                        return null;
                    }

                    @Override
                    public void setFullName(String s) {

                    }

                    @Override
                    public Iterator<Group> getGroups() {
                        return null;
                    }

                    @Override
                    public String getPassword() {
                        return null;
                    }

                    @Override
                    public void setPassword(String s) {

                    }

                    @Override
                    public Iterator<Role> getRoles() {
                        return null;
                    }

                    @Override
                    public UserDatabase getUserDatabase() {
                        return null;
                    }

                    @Override
                    public String getUsername() {
                        return null;
                    }

                    @Override
                    public void setUsername(String s) {

                    }

                    @Override
                    public void addGroup(Group group) {

                    }

                    @Override
                    public void addRole(Role role) {

                    }

                    @Override
                    public boolean isInGroup(Group group) {
                        return false;
                    }

                    @Override
                    public boolean isInRole(Role role) {
                        return false;
                    }

                    @Override
                    public void removeGroup(Group group) {

                    }

                    @Override
                    public void removeGroups() {

                    }

                    @Override
                    public void removeRole(Role role) {

                    }

                    @Override
                    public void removeRoles() {

                    }
                };
                user.setUsername(name);
                user.setPassword(password);

                userList.add(user);

            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userList;
    }
}